/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utils;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.swing.JPanel;
import pojo.Chongoi;
import pojo.Lichchieuphim;
import pojo.Phongchieu;

/**
 *
 * @author Hnmtuan
 */
public class ClassUtils {

    public final static boolean IMAGE_AVATAR = true;
    public final static boolean IMAGE_NO_AVATAR = false;
    public final static String ERR_INVALID_PARAMS = "Paramter invalid !";
    public final static String ERR_LIST_EMPTY = "Collection is empty !";
    public final static String ERR_NOT_DIRECTORY = "%s not directory !";
    public final static String ERR_NOT_FILE = "%s not file !";
    public final static String ERR_FILE_NOT_FOUND = "File %s not found !";
    public final static String ERR_DIRECTORY_NOT_FOUND = "Folder %s not found !";
    public final static String UNKNOWN = "U";
    public final static int COPY_FAILED = 0;
    public final static int COPY_SUCCESS = 1;

    //  <editor-fold defaultstate="collapsed" desc="Define class state field" >
    public class StateFields {

        public static final int DELETE_STATUS = 0;
        public static final int DEFAULT_STATUS = 1;
    }
    // </editor-fold>

    public class PlaceTypes {

        public static final int STANDARD_TYPE = 0;
        public static final int VIP_TYPE = 1;
    }

    public class PlacePrices {

        public static final float STANDARD_PRICE = 1000;
        public static final float VIP_PRICE = 2000;
    }

    //  <editor-fold defaultstate="collapsed" desc="Define class film versions" >
    public static class FilmVersions {

        private int _msgShort;
        private String _msgLong;

        public String getMsgLong() {
            return _msgLong;
        }

        public int getMsgShort() {
            return _msgShort;
        }

        public FilmVersions(int msgShort, String msgLong) {
            _msgShort = msgShort;
            _msgLong = msgLong;
        }
    }
    // </editor-fold>

    public static List<FilmVersions> getAllVersions() throws Exception {
        List<FilmVersions> versions = new ArrayList<FilmVersions>();
        versions.add(new FilmVersions(1, "Film 2D"));
        versions.add(new FilmVersions(2, "Film 3D"));
        return versions;
    }

    public static Object getFirstItemInSet(Set collections) throws Exception {
        Object result = null;
        if (collections == null) {
            throw new Exception(ERR_INVALID_PARAMS);
        }
        if (collections.isEmpty()) {
            throw new Exception(ERR_LIST_EMPTY);
        }
        Iterator it = collections.iterator();
        if (it.hasNext()) {
            result = it.next();
        }
        return result;
    }

    public static String getCurrentApplication() throws Exception {
        return System.getProperty("user.dir");
    }

    public static int copyFileToDirectory(String sourceFile, String destDirectory) throws Exception {
        int result = COPY_FAILED;
        if (sourceFile == null || destDirectory == null || sourceFile.isEmpty() || destDirectory.isEmpty()) {
            throw new Exception(ERR_INVALID_PARAMS);
        }
        File localSourceFile = new File(sourceFile);
        File localDestDirectory = new File(destDirectory);
        if (!localSourceFile.exists()) {
            throw new Exception(String.format(ERR_FILE_NOT_FOUND, localSourceFile.getName()));
        }
        if (!localSourceFile.isFile()) {
            throw new Exception(String.format(ERR_NOT_FILE, localSourceFile.getName()));
        }
        if (!localDestDirectory.exists()) {
            throw new Exception(String.format(ERR_DIRECTORY_NOT_FOUND, localDestDirectory.getName()));
        }
        if (!localDestDirectory.isDirectory()) {
            throw new Exception(String.format(ERR_NOT_DIRECTORY, localDestDirectory.getName()));
        }
        InputStream fin = new FileInputStream(localSourceFile);
        // >>> throw exception access denies
        OutputStream fout = new FileOutputStream(localDestDirectory);

        byte[] buf = new byte[1024];
        int len;
        while ((len = fin.read(buf)) > 0) {
            fout.write(buf, 0, len);
        }
        fin.close();
        fout.close();
        result = COPY_SUCCESS;
        return result;
    }

    public static JPanel generatePlacesUI(Phongchieu paramRoom, List<Chongoi> paramPlaces) throws Exception {
        if (paramRoom == null || paramPlaces == null) {
            throw new Exception(ERR_INVALID_PARAMS);
        }
        int placeSize = paramPlaces.size();
        if (placeSize < 1) {
            throw new Exception(ERR_INVALID_PARAMS);
        }
        JPanel componentParent = new JPanel();
        Phongchieu localParamRoom = paramRoom;
        int col = localParamRoom.getSoLuongCot();
        int line = localParamRoom.getSoLuongHangGhe();
        int wPanel = PlaceButton.DEFAULT_WIDTH * col;
        int hPanel = PlaceButton.DEFAULT_HEIGHT * line;
        componentParent.setPreferredSize(new Dimension(wPanel, hPanel));
        componentParent.setLayout(new FlowLayout(FlowLayout.LEFT));
//        for (int i = 0; i < localLine; i++) {
//            for (int j = 0; j < localCol; j++) {
//                JButton bt = new PlaceButton(String.format("%d / %d", i, j));
//                componentParent.add(bt);
//            }
//        }
        for (Chongoi chongoi : paramPlaces) {
            PlaceButton temp = new PlaceButton(ERR_NOT_FILE, chongoi);
            componentParent.add(temp);
        }
        return componentParent;
    }

    public static JPanel generateBookingButtonUI(Phongchieu paramRoom, List<Chongoi> paramPlaces, Lichchieuphim paramSchedules) throws Exception {
        if (paramRoom == null || paramPlaces == null || paramSchedules == null) {
            throw new Exception(ERR_INVALID_PARAMS);
        }
        int placeSize = paramPlaces.size();
        if (placeSize < 1) {
            throw new Exception(ERR_INVALID_PARAMS);
        }
        JPanel componentParent = new JPanel();
        Phongchieu localParamRoom = paramRoom;
        Lichchieuphim localSchedules = paramSchedules;
        int col = localParamRoom.getSoLuongCot();
        int line = localParamRoom.getSoLuongHangGhe();
        int wPanel = PlaceButton.DEFAULT_WIDTH * col;
        int hPanel = PlaceButton.DEFAULT_HEIGHT * line;
        componentParent.setPreferredSize(new Dimension(wPanel, hPanel));
        componentParent.setLayout(new FlowLayout(FlowLayout.LEFT));
        for (Chongoi chongoi : paramPlaces) {
            BookingButton temp = new BookingButton(chongoi, localSchedules);
            componentParent.add(temp);
        }
        return componentParent;
    }
}
